
<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>
 by Seyed Amir Mirsalimi<br />
Type an expression in the following box (e.g., 10.5+20*3/25).
<p>
<form method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
</p>
<?php
$x = $_GET["expr"];
$count =0;
// -- is invalid
$count+=preg_match('/[\-][\-]/',$x);
//no division by 0
$count+=preg_match('/\/0/',$x);
//cant start with +,*,/
$count+=preg_match('/^[\*,\/,\+]/',$x);
// can have 2 operators in a row
$count+=preg_match('/[\*,\/,\+,\-]\s*[\*,\/,\+]/',$x);
// cant have any thing but digits, . , /, * , -, + , /
$count+=preg_match('/[^0-9,\*,\/,\+,\-,\.,\s]/',$x);
//cant have a number with 2 decimal points 
$count+=preg_match('/[0-9]*[\.][0-9,\s]*[\.]/',$x);
//cant end with an operator
$count+=preg_match('/[\+,\-,\*,\/]$/',$x); 

?>
<ul>
<li>Only numbers and +,-,* and / operators are allowed in the expression.
<li>The evaluation follows the standard operator precedence.
<li>The calculator does not support parentheses.
<li>The calculator handles invalid input "gracefully". It does not output PHP error messages.
</ul>
<?php
//print $count;
if($count == 0){
	eval("\$y =".$x .";");
	$y = $x ." = ". $y;
	}
else
	$y = "invalid expression: ".$x ;
print $y;
?>
</body>
</html>